package com.google.masai;



import java.io.PrintWriter;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dropbox.client.DropboxAPI;
import com.dropbox.client.DropboxAPI.Config;
import com.dropbox.client.DropboxAPI.Entry;


public class Upload extends Activity {
    private static final String TAG = "DropboxSample";

    // Replace this with your consumer key and secret assigned by Dropbox.
    // Note that this is a really insecure way to do this, and you shouldn't
    // ship code which contains your key & secret in such an obvious way.
    // Obfuscation is good.
    final static private String CONSUMER_KEY = "1ulu2y4f1e4cuon";
    final static private String CONSUMER_SECRET = "9a1hxrnd9zolyo1";

    private DropboxAPI api = new DropboxAPI();

    final static public String ACCOUNT_PREFS_NAME = "prefs";
    final static public String ACCESS_KEY_NAME = "ACCESS_KEY";
    final static public String ACCESS_SECRET_NAME = "ACCESS_SECRET";

    private boolean mLoggedIn;
    private EditText mLoginEmail;
    private EditText mLoginPassword;
    private Button mSubmit;
    private TextView mText;
    private Config mConfig;
    
    private DropBox dropbox; 
    
    
    /*Debugging key store token for my fake db account*/
    //private String[] keyTokens = {"le22emztwncgzwx", "3fu0ztxgw5v87fo"}; 
    private String[] keyTokens = {"p7e07spqj3j2ds0", "5dtg61v48x3ukms"};
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String usr = "thisisnotmyrealemail.com@thisisnotmyrealemail.com";
        String pass = "thisisnotmyrealemail.com";
        
        EditText userBox = (EditText) findViewById(R.id.usernameBox);
        EditText passBox = (EditText) findViewById(R.id.passwordBox);
        
        userBox.setText(usr);
        passBox.setText(pass);
        
        
        logText("Hello mamamia");
        
        DropBox db =null;
         
        try {
        
        	db = new DropBox(keyTokens);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logText((e.toString()));
			
			e.printStackTrace();
		
			
		
			
		} 
        
		if(db == null){
			showToast("Dropbox is null"); 
		}
        this.dropbox = db; 
        
        String uploadDir = "/public";
        uploadDir = "/public/testfolder";
        String path = "/mnt/sdcard/Download/ReSume.pdf"; 
        //dropbox.setUpload(uploadDir, path); 
        
        UploadAsync upload = new UploadAsync(this, path, uploadDir); 
        showToast("Starting Async Dropbox upload....");
        upload.execute(dropbox);
        
        
        //String dropboxFilePath = "/public/Resume.pdf"; 
        String dropboxFilePath = "/public/solutionsamplefinal.pdf";
        String localFilePath = "/mnt/sdcard/Download/final2.pdf";
        
        //DownloadAsync dwn = new DownloadAsync(this, dropboxFilePath, localFilePath);
        //dwn.execute(dropbox);
        
        //
        for (Entry e : dropbox.getFiles("/public")){
        	logText(e.fileName());
        }
        
        
        //testDBLogin(); - get new key pair 
        
    }

    
    public void testDBLogin(){

    	String usr = "thisisnotmyrealemail.com@thisisnotmyrealemail.com";
        String pass = "thisisnotmyrealemail.com";
        

    	DropBox db = new DropBox(usr,pass); 
        String[] keys = db.getStoredTokens(); 
        
        logText(keys[0]);
        logText(keys[1]);
        
        Log.d("DROPBOX TAG", keys[0]);
        Log.d("DROPBOX TAG", keys[1]);
        
        this.dropbox = db; 
    }
    
    public void logText(String msg){
    	
    	TextView txtBox = (TextView) (findViewById(R.id.log));
    	String temp =  txtBox.getText().toString();
    	temp += "\n"+msg;
    	txtBox.setText(temp);
    	
    }
    
    
    private void login(){
    	//showToast("HELLO - Button Pressed");
    	
   	 mLoginEmail = (EditText)findViewById(R.id.usernameBox);
        mLoginPassword = (EditText)findViewById(R.id.passwordBox);
        mSubmit = (Button)findViewById(R.id.Login);
        mText = (TextView)findViewById(R.id.loginStatusMsg);
        
        if (mLoggedIn) {
    		// We're going to log out
    		api.deauthenticate();
    		clearKeys();
    		setLoggedIn(false);
    		mText.setText("");
    	} else {
    		// Try to log in
    		getAccountInfo();
    	}
        
        /*mSubmit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	if (mLoggedIn) {
            		// We're going to log out
            		api.deauthenticate();
            		clearKeys();
            		setLoggedIn(false);
            		mText.setText("");
            	} else {
            		// Try to log in
            		getAccountInfo();
            	}
            }
        });*/ 
        
        String[] keys = getKeys();
        if (keys != null) {
        	setLoggedIn(true);
        	Log.i(TAG, "Logged in already");
        } else {
        	setLoggedIn(false);
        	Log.i(TAG, "Not logged in");
        }
        if (authenticate()) {
        	// We can query the account info already, since we have stored 
        	// credentials
        	getAccountInfo();
        }
   	
    }
    
    public void onClick(View v){
    	
    	switch (v.getId()) {
		case R.id.Login:
			login();
			break;
			
		case R.id.ListFiles:
			showToast("helllo list");
			///return;
			dropbox.getFiles("/");
			String path = "/public";
			for(Entry e : dropbox.getFiles(path)){
				logText(e.fileName());
			}
			
			break; 
		}
    	
    }
    
    
    /**
     * This lets us use the Dropbox API from the LoginAsyncTask
     */
    public DropboxAPI getAPI() {
    	return api;
    }

    /**
     * Convenience function to change UI state based on being logged in
     */
    public void setLoggedIn(boolean loggedIn) {
    	mLoggedIn = loggedIn;
    	mLoginEmail.setEnabled(!loggedIn);
    	mLoginPassword.setEnabled(!loggedIn);
    	if (loggedIn) {
    		mSubmit.setText("Log Out of Dropbox");
    	} else {
    		mSubmit.setText("Log In to Dropbox");
    	}
    }

    public void showToast(String msg) {
        Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
        error.show();
    }
    
    private void getAccountInfo() {
    	if (api.isAuthenticated()) {
    		// If we're already authenticated, we don't need to get the login info
	        LoginAsyncTask login = new LoginAsyncTask(this, null, null, getConfig());
	        login.execute();    		
    	} else {
    	
	        String email = mLoginEmail.getText().toString();
	        if (email.length() < 5 || email.indexOf("@") < 0 || email.indexOf(".") < 0) {
	            showToast("Error, invalid e-mail");
	            return;
	        }
	
	        String password = mLoginPassword.getText().toString();
	        if (password.length() < 6) {
	            showToast("Error, password too short");
	            return;
	        }

	        // It's good to do Dropbox API (and any web API) calls in a separate thread,
	        // so we don't get a force-close due to the UI thread stalling.
	        LoginAsyncTask login = new LoginAsyncTask(this, email, password, getConfig());
	        login.execute();
    	}
    }

    /**
     * Displays some useful info about the account, to demonstrate
     * that we've successfully logged in
     * @param account
     */
    public void displayAccountInfo(DropboxAPI.Account account) {
    	if (account != null) {
    		String info = "Name: " + account.displayName + "\n" +
    			"E-mail: " + account.email + "\n" + 
    			"User ID: " + account.uid + "\n" +
    			"Quota: " + account.quotaQuota;
    		mText.setText(info);
    	}
    }
    
    /**
     * This handles authentication if the user's token & secret
     * are stored locally, so we don't have to store user-name & password
     * and re-send every time.
     */
    protected boolean authenticate() {
    	if (mConfig == null) {
    		mConfig = getConfig();
    	}
    	String keys[] = getKeys();
    	if (keys != null) {
	        mConfig = api.authenticateToken(keys[0], keys[1], mConfig);
	        if (mConfig != null) {
	            return true;
	        }
    	}
    	showToast("Failed user authentication for stored login tokens.");
    	clearKeys();
    	setLoggedIn(false);
    	return false;
    }
    
    protected Config getConfig() {
    	if (mConfig == null) {
	    	mConfig = api.getConfig(null, false);
	    	// TODO On a production app which you distribute, your consumer
	    	// key and secret should be obfuscated somehow.
	    	mConfig.consumerKey=CONSUMER_KEY;
	    	mConfig.consumerSecret=CONSUMER_SECRET;
	    	mConfig.server="api.dropbox.com";
	    	mConfig.contentServer="api-content.dropbox.com";
	    	mConfig.port=80;
    	}
    	return mConfig;
    }
    
    public void setConfig(Config conf) {
    	mConfig = conf;
    }
    
    /**
     * Shows keeping the access keys returned from Trusted Authenticator in a local
     * store, rather than storing user name & password, and re-authenticating each
     * time (which is not to be done, ever).
     * 
     * @return Array of [access_key, access_secret], or null if none stored
     */
    public String[] getKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        String key = prefs.getString(ACCESS_KEY_NAME, null);
        String secret = prefs.getString(ACCESS_SECRET_NAME, null);
        if (key != null && secret != null) {
        	String[] ret = new String[2];
        	ret[0] = key;
        	ret[1] = secret;
        	return ret;
        } else {
        	return null;
        }
    }
    
    /**
     * Shows keeping the access keys returned from Trusted Authenticator in a local
     * store, rather than storing user name & password, and re-authenticating each
     * time (which is not to be done, ever).
     */
    public void storeKeys(String key, String secret) {
        // Save the access key for later
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, key);
        edit.putString(ACCESS_SECRET_NAME, secret);
        edit.commit();
    }
    
    public void clearKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.clear();
        edit.commit();
    }    	
}